<template>
  <div class="app-container">
    <!-- 顶部搜索 -->
    <el-form :model="queryParams" ref="queryForm" size="small" v-show="showSearch" label-width="80px">
      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item label="企业名称:" prop="qyName">
            <el-input
              v-model="queryParams.qyName"
              placeholder="请输入企业名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="年度:" prop="nd">
            <el-select v-model="queryParams.nd" placeholder="请选择年度" clearable>
              <el-option
                v-for="dict in dict.type.year"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <!-- 顶部按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['jjkc:qyjbxx:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['jjkc:qyjbxx:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['jjkc:qyjbxx:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['jjkc:qyjbxx:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 表格 -->
    <el-table @row-click="clickRow" ref="table" highlight-current-row
              :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold'}"
              height="300" class="fixedTableOne" v-loading="loading" :data="qyjbxxList"
              @selection-change="handleSelectionChange">
      <!--      <el-table-column type="selection" width="55" align="center"/>-->
      <el-table-column label="序号" align="center" type="index" :index="indexMethod"/>
      <el-table-column show-overflow-tooltip label="企业名称(盖章)" align="center" prop="qyName"/>
      <el-table-column show-overflow-tooltip label="法人代表" align="center" prop="frDb"/>
      <el-table-column show-overflow-tooltip label="通讯地址" align="center" prop="txDz"/>
      <el-table-column show-overflow-tooltip label="企业联系电话(座机)" align="center" prop="qyLxDh"/>
      <el-table-column show-overflow-tooltip label="年度" align="center" prop="nd">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.year" :value="scope.row.nd"/>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="企业类型" align="center" prop="qyLx"/>
      <el-table-column show-overflow-tooltip label="行业名称" align="center" prop="hyMc"/>
      <el-table-column show-overflow-tooltip label="类别" align="center" prop="lb"/>
      <el-table-column show-overflow-tooltip label="职工总人数" align="center" prop="zgZrs"/>
      <el-table-column show-overflow-tooltip label="研发人员数" align="center" prop="yfRys"/>
      <el-table-column show-overflow-tooltip label="年度销售收入(万元)" align="center" prop="ndXsSr"/>
      <el-table-column show-overflow-tooltip label="年度利税(万元)" align="center" prop="ndLs"/>
      <el-table-column show-overflow-tooltip label="联系人" align="center" prop="lxr"/>
      <el-table-column show-overflow-tooltip label="联系电话" align="center" prop="lxDh"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['jjkc:qyjbxx:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['jjkc:qyjbxx:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改企业基本信息对话框 -->
    <vxe-modal :title="title" v-model="open" width="60%" showFooter show-zoom resize>
      <el-form ref="formDialog" :model="form" :rules="rules" label-width="150px">
        <div style="height: 50vh;">
          <!-- 基本资料 -->
          <el-card class="box-card">
            <el-row :gutter="24">
              <el-col :span="12">
                <el-form-item label="企业名称(盖章)" prop="qyName">
                  <el-input v-model="form.qyName" placeholder="请输入企业名称(盖章)" class="custom-input"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="法人代表" prop="frDb">
                  <el-input v-model="form.frDb" placeholder="请输入法人代表" class="custom-input"/>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="24">
              <el-col :span="12">
                <el-form-item label="通讯地址" prop="txDz">
                  <el-input v-model="form.txDz" placeholder="请输入通讯地址" class="custom-input"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="企业联系电话(座机)" prop="qyLxDh">
                  <el-input v-model="form.qyLxDh" placeholder="请输入企业联系电话(座机)" class="custom-input"/>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="24">
              <el-col :span="12">
                <el-form-item label="年度" prop="nd">
                  <el-select v-model="form.nd" placeholder="请选择年度" class="custom-input">
                    <el-option
                      v-for="dict in dict.type.year"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="企业类型" prop="qyLx">
                  <el-input v-model="form.qyLx" placeholder="请输入企业类型" class="custom-input"/>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="24">
              <el-col :span="12">
                <el-form-item label="行业名称" prop="hyMc">
                  <el-input v-model="form.hyMc" placeholder="请输入行业名称"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="类别" prop="lb">
                  <el-input v-model="form.lb" placeholder="请输入类别"/>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="24">
              <el-col :span="12">
                <el-form-item label="职工总人数" prop="zgZrs">
                  <el-input v-model="form.zgZrs" placeholder="请输入职工总人数"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="研发人员数" prop="yfRys">
                  <el-input v-model="form.yfRys" placeholder="请输入研发人员数"/>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="24">
              <el-col :span="12">
                <el-form-item label="年度销售收入(万元)" prop="ndXsSr">
                  <el-input type="number" v-model="form.ndXsSr" placeholder="请输入年度销售收入(万元)"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="年度利税(万元)" prop="ndLs">
                  <el-input type="number" v-model="form.ndLs" placeholder="请输入年度利税(万元)"/>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="24">
              <el-col :span="12">
                <el-form-item label="联系人" prop="lxr">
                  <el-input v-model="form.lxr" placeholder="请输入联系人"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="联系电话" prop="lxDh">
                  <el-input v-model="form.lxDh" placeholder="请输入联系电话"/>
                </el-form-item>
              </el-col>
            </el-row>
          </el-card>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </vxe-modal>
  </div>
</template>

<script>
import {addQyjbxx, delQyjbxx, getQyjbxx, getQyJbXxId, listQyjbxx, updateQyjbxx} from "@/api/jjkc/qyjbxx";

export default {
  name: "Qyjbxx",
  dicts: ['year'],
  data() {
    return {
      getQyJbXxId: null, //从后端获取到的企业基本信息主键ID (UUID)
      selectedRow: null, // 保存当前选中的行
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业基本信息表格数据
      qyjbxxList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        qyjbxxId: null,
        qyName: null,
        nd: null,
        userId: null,
        deptId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        qyName: [{required: true, message: "企业名称(盖章)不能为空", trigger: "blur"}],
        frDb: [{required: true, message: "法人代表不能为空", trigger: "blur"}],
        txDz: [{required: true, message: "通讯地址不能为空", trigger: "blur"}],
        qyLxDh: [{required: true, message: "企业联系电话(座机)不能为空", trigger: "blur"}],
        nd: [{required: true, message: "年度不能为空", trigger: "change"}],
        qyLx: [{required: true, message: "企业类型不能为空", trigger: "blur"}],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //点击行 获取行
    clickRow(row) {
      this.selectedRow = row; // 更新选中的行
      const table = this.$refs.table;
      const selectedRows = table.selection;

      // 否则取消之前选中的行，并选中当前点击的行
      selectedRows.forEach(selectedRow => {
        table.toggleRowSelection(selectedRow);
      });
      table.toggleRowSelection(row);
    },
    /** 自定义序号 */
    indexMethod(index) {
      let pageNum = this.queryParams.pageNum - 1;
      if ((pageNum !== -1 && pageNum !== 0)) {
        return (index + 1) + (pageNum * this.queryParams.pageSize);
      } else {
        return (index + 1)
      }
    },
    /** 查询企业基本信息列表 */
    getList() {
      this.loading = true;
      listQyjbxx(this.queryParams).then(response => {
        this.qyjbxxList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        qyjbxxId: this.getQyJbXxId,
        qyName: null,
        frDb: null,
        txDz: null,
        qyLxDh: null,
        nd: null,
        qyLx: null,
        hyMc: null,
        lb: null,
        zgZrs: null,
        yfRys: null,
        ndXsSr: null,
        ndLs: null,
        lxr: null,
        lxDh: null,
        idWhere: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        userId: null,
        deptId: null
      };
      this.resetForm("formDialog");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.qyjbxxId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      getQyJbXxId().then(res => {
        this.getQyJbXxId = res.msg
        this.reset();
      })
      this.open = true;
      this.title = "添加企业基本信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const qyjbxxId = row.qyjbxxId || this.ids
      getQyjbxx(qyjbxxId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改企业基本信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["formDialog"].validate(valid => {
        if (valid) {
          if (this.form.idWhere != null) {
            updateQyjbxx(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addQyjbxx(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const qyjbxxIds = row.qyjbxxId || this.ids;
      this.$modal.confirm('确定删除选中记录？').then(function () {
        return delQyjbxx(qyjbxxIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('jjkc/qyjbxx/export', {
        ...this.queryParams
      }, `qyjbxx_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>


<style scoped>
.custom-input ::v-deep .el-input__inner {
  background-color: #ffffe6;
}
</style>
